<?php

namespace System\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use System\Model\Catalog;
use Zend\Session\Container;

class CatalogTable extends AbstractTableGateway {

    protected $table = "tblcatalog";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Catalog());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function loadcatalog($parentId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        if ($parentId > -1) {
            $sqlEx->where(array("catalogParent" => $parentId));
        }
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $result) {
            $data[] = $result;
        }
        return $data;
    }

    public function addCatalog(Catalog $objCata) {
        $data = array();
        $data["catalogName"] = $objCata->catalogName;
        $data["catalogDes"] = $objCata->catalogDes;
        $data["catalogParent"] = $objCata->catalogParent;
        $link = $this->checklinkcata($objCata->catalogLink);
        $data["catalogLink"] = $link;
        $data["catalogDate"] = $objCata->catalogDate;
        $data["catalogUpdate"] = $objCata->catalogUpdate;
        $data["catalogActive"] = $objCata->catalogActive;
        $data["catalogSort"] = $objCata->catalogSort;
        $sqlEx = $this->sql->insert();
        $sqlEx->into($this->table);
        $sqlEx->values($data);
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $checkadd = $pst->execute();
        if ($checkadd != null) {
            return true;
        } else {
            return false;
        }
    }

    private function checklinkcata($link) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);

        $sqlEx->where(array("catalogLink" => $link));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = $result->count();
        if ($data > 1) {
            return $link = $link . "1";
        } else {
            return $link;
        }
    }

    public function scanlistparentcata($rootmenu) {
        $index = 1;

        $listcataId = array();
        $listcataId[0] = $rootmenu;
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogId" => "catalogId"));
        $sqlEx->where(array("catalogParent" => $rootmenu));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $results) {
            $data[] = $results;
        }
        $arr = array();
        $index2 = 0;
        $def = "";
        foreach ($data as $key => $scan) {
            array_push($listcataId, $scan["catalogId"]);
            $strr = $this->scacsubmenu($index + 1, $scan["catalogId"], $listcataId, $def);
            $arr[$index2] = $strr;
            $index2++;
        }
        $hello = implode("-", $arr);
        return $hello;
    }

    public function scacsubmenu($index, $cataID, $listcataId, $arr) {

        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogId" => "catalogId"));
        $sqlEx->where(array("catalogParent" => $cataID));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $results) {
            $data[] = $results;
        }
        foreach ($data as $key => $scan) {
            array_push($listcataId, $scan["catalogId"]);
            $arr.=$this->scacsubmenu($index + 1, $scan["catalogId"], $listcataId, $arr) . "-";
        }


        $arr .= implode("-", $listcataId);
        return $arr;
    }

    public function delcata($data) {
        $sqlEx = $this->sql->delete();
        $sqlEx->from($this->table);
        $sqlEx->where(array("catalogId" => $data));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        try {
            $pst->execute();
        } catch (Exception $ex) {
            throw new Exception("loi roi");
        }
        return true;
    }

    public function checkdelvideos($data) {
        $sqlEx = $this->sql->select();
        $sqlEx->from("tblvideos");
        $sqlEx->columns(array("videoImg" => "videoImg", "videoImgThumb" => "videoImgThumb"));
        $sqlEx->where(array("catalogId" => $data));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $data = array();
        foreach ($rs as $results) {
            $data[] = $results;
        }
        if (count($data) > 0) {
            return $data;
        } else {
            return "";
        }
    }

    public function delImgVideos($data) {
        foreach ($data as $key => $listImg) {
            $urlImg = $data["videoImg"];
            $urlImgThumb = $data["videoImgThumb"];
            $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/");
            @unlink($dirpath . $urlImg);
            @unlink($dirpath . $urlImgThumb);
        }
    }

    public function delvideos($data) {
        $sqlEx = $this->sql->delete();
        $sqlEx->from("tblvideos");
        $sqlEx->where(array("catalogId" => $data));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

    public function updateActive(Catalog $objCata) {
        $catalogId = $objCata->getCataId();
        $cataactive = $objCata->getCataActive();
        $data = array();
        $data["catalogActive"] = $cataactive;
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("catalogId" => $catalogId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

    public function updateparent(Catalog $objCata, $cataparent) {
        $catalogId = $objCata->getCataId();
        //$cataparent = $objCata->getCataParent();
        //echo $cataparent;
        $data = array();
        $data["catalogParent"] = $cataparent;
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("catalogId" => $catalogId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

    public function loaddataedit($catalogId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("catalogName" => "catalogName", "catalogDes" => "catalogDes"));
        $sqlEx->where(array("catalogId" => $catalogId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $data = $rs->current();
        return $data;
    }

    public function updateCatalog(Catalog $objCata, $catalogId) {
        $data = array();
        $data["catalogName"] = $objCata->catalogName;
        $data["catalogDes"] = $objCata->catalogDes;
        $link = $this->checklinkcata($objCata->catalogLink);
        $data["catalogLink"] = $link;
        $data["catalogUpdate"] = $objCata->catalogUpdate;
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("catalogId" => $catalogId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $checkadd = $pst->execute();
        if ($checkadd != null) {
            return true;
        } else {
            return false;
        }
    }

    public function showindex($catalogId, $flag) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set(array("showindex" => $flag));
        $sqlEx->where(array("catalogId" => $catalogId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

}
